package cn.com.user.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cabinet.po.IctCommoditiesQuery;
import com.cabinet.po.IctUser;
import com.cabinet.service.ILatticeService;
import com.cabinet.service.IMCommoditiesService;
import com.cabinet.service.IMUserService;
import com.cabinet.web.entity.Commodities;
import com.cabinet.web.entity.Lattice;

import cn.com.easy.utils.CookieUtils;
import cn.com.easy.utils.ResponseOutputUtils;

@Controller
@RequestMapping("/commodities")
public class CommoditiesController {
	@Autowired
	private IMUserService iMUserService;
	
	@Autowired
	private ILatticeService iLatticeService;
	
	@Autowired
	private IMCommoditiesService iMCommoditiesService;


	@RequestMapping
	public String getcommodities(ModelMap ModelMap,HttpServletRequest request) throws NumberFormatException, Exception{
		//String cookiecid = 
		if(CookieUtils.getCookieValue(request, "cid") == null || CookieUtils.getCookieValue(request, "cid").equals("null")){
			return "redirect:/near/by";
		}else{
			String cid = CookieUtils.getCookieValue(request, "cid");
			int cclass = Integer.valueOf(CookieUtils.getCookieValue(request, "cid").substring(0, 1));
			if(cclass == 1){
				IctCommoditiesQuery ictCommoditiesQuery = new IctCommoditiesQuery();
				ictCommoditiesQuery.setCabId(cid);
				List<IctCommoditiesQuery> ictCommoditiesQueryList = iMCommoditiesService.queryCommoditiesByCidAndClaName(ictCommoditiesQuery);
				ModelMap.addAttribute("ictCommoditiesQueryList", ictCommoditiesQueryList);
				return "/commodities/commodities";
			}else{
				List<Lattice> latticeList = iLatticeService.getLatticeListByCid(cid.toString());
				ModelMap.addAttribute("latticeList", latticeList);
				return "/ceposit/ceposit";
			}
				/*for (IctCommoditiesQuery ictCommoditiesQuery2 : i) {
					System.out.println(ictCommoditiesQuery2.getCoName());
				}*/
		}
	}
	
	
	@RequestMapping("/detail")
	public String getCommoditiesDetail(Long latid,ModelMap modelMap){
		Lattice lattice = iLatticeService.getLattice(latid);
		Commodities commodities = iMCommoditiesService.getCommoditiesDetail(lattice.getCommoditiesId());
		if(lattice.getSalePrice()!=null){
			commodities.setCoPrice(lattice.getSalePrice());
		}
		modelMap.addAttribute("commoditiesDetail", commodities);
		modelMap.addAttribute("latid", latid);
		return "/commodities/commodities_detail";
		
	}
	
	
	@RequestMapping("/search")
	@ResponseBody
	public List<IctCommoditiesQuery> searchCommodities(ModelMap ModelMap,HttpServletRequest request,String claname) throws NumberFormatException, Exception{

				String cid = CookieUtils.getCookieValue(request, "cid");
			
				IctCommoditiesQuery ictCommoditiesQuery = new IctCommoditiesQuery();
				//System.out.println(claname);
				if(claname.equals("drink")){
					ictCommoditiesQuery.setClaname("饮料");
				}else{
					if(claname.equals("food")){
						ictCommoditiesQuery.setClaname("零食");
					}
				}
				ictCommoditiesQuery.setCabId(cid);
				List<IctCommoditiesQuery> ictCommoditiesQueryList = iMCommoditiesService.queryCommoditiesByCidAndClaName(ictCommoditiesQuery);
				//ModelMap.addAttribute("ictCommoditiesQueryList", ictCommoditiesQueryList);
				
				return ictCommoditiesQueryList;
				/*for (IctCommoditiesQuery ictCommoditiesQuery2 : i) {
					System.out.println(ictCommoditiesQuery2.getCoName());
				}*/

		
		/*IctCommoditiesQuery ictCommoditiesQuery = new IctCommoditiesQuery();
		ictCommoditiesQuery.setCabId(101l);
		List<IctCommoditiesQuery> i = iMCommoditiesService.queryCommoditiesByCidAndClaName(ictCommoditiesQuery);
		for (IctCommoditiesQuery ictCommoditiesQuery2 : i) {
			System.out.println(ictCommoditiesQuery2.getCoName());
		}*/
	}
	
	@RequestMapping("/test")
	public void test(){
		IctCommoditiesQuery ictCommoditiesQuery = new IctCommoditiesQuery();
		ictCommoditiesQuery.setCabId("101");
		List<IctCommoditiesQuery> i = iMCommoditiesService.queryCommoditiesByCidAndClaName(ictCommoditiesQuery);
		for (IctCommoditiesQuery ictCommoditiesQuery2 : i) {
			System.out.println(ictCommoditiesQuery2.getCoName());
		}
	}
	
	
	
	@RequestMapping("/list")
	public String getcommoditiestest(ModelMap ModelMap,HttpServletRequest request) throws NumberFormatException, Exception{
	
			String cid = "120170228152108";
			
				IctCommoditiesQuery ictCommoditiesQuery = new IctCommoditiesQuery();
				ictCommoditiesQuery.setCabId(cid);
				List<IctCommoditiesQuery> ictCommoditiesQueryList = iMCommoditiesService.queryCommoditiesByCidAndClaName(ictCommoditiesQuery);
				ModelMap.addAttribute("ictCommoditiesQueryList", ictCommoditiesQueryList);
				return "/commodities/commodities";

	}
	
}
